﻿Library.Ajax.WebMethod = {
    Create: function (url, postScriptName) {
        Library.Ajax.Create();
        if (ajaxRequest) {
            var paramUrl = Library.Ajax.ReplaceParameters(url);
            ajaxRequest.onreadystatechange = function () {
                if (Library.Ajax.Status()) {
                    var xmlResponse = Library.Ajax.ResponseParser();
                    if (xmlResponse == null)
                        return;

                    if (xmlResponse.getElementsByTagName("name")[0].firstChild.data == "Library.Ajax.InfoMessage" && xmlResponse.getElementsByTagName("value")[0].firstChild.data == "OK")
                        if (postScriptName != null && postScriptName != '')
                            eval(postScriptName);
                    
                }
            }
            ajaxRequest.open("get", paramUrl, true);
            ajaxRequest.send(null);
        }
        Library.Ajax.Progress(false);
    }
};

/*
* 
* TEST = OK
*/
Library.Ajax.ComboBox = {
    Create: function (sourceControlId, targetControlId, url, postScriptName) {
        Library.UserInterface.GetDetailPageElement(sourceControlId).onchange = function () {
            Library.Ajax.Create();
            if (ajaxRequest) {
                var paramUrl = Library.Ajax.ReplaceParameters(url);
                ajaxRequest.onreadystatechange = function () {
                    if (Library.Ajax.Status()) {
                        var xmlResponse = Library.Ajax.ResponseParser();
                        if (xmlResponse == null)
                            return;

                        var comboBox = Library.UserInterface.GetDetailPageElement(targetControlId);

                        comboBox.options.length = 0;
                        comboBox.options.add(new Option('-----', ''));

                        if (xmlResponse != null) {
                            for (var i = 0; i < xmlResponse.getElementsByTagName("name").length; i++) {
                                comboBox.options.add(new Option(xmlResponse.getElementsByTagName("name")[i].firstChild.data, xmlResponse.getElementsByTagName("value")[i].firstChild.data));
                            }
                        }
                        if (postScriptName != null && postScriptName != '') {
                            eval(postScriptName);
                        }
                    }
                }
                ajaxRequest.open("get", paramUrl, true);
                ajaxRequest.send(null);
            }
            Library.Ajax.Progress(false);
        }                
    }
};

/*
* 
* TEST = OK
*/
Library.Ajax.UpdateField = {
    Create: function (url, postScriptName) {
        Library.Ajax.Create();
        if (ajaxRequest) {
            var paramUrl = Library.Ajax.ReplaceParameters(url);
            ajaxRequest.onreadystatechange = function () {
                if (Library.Ajax.Status()) {
                    var xmlResponse = Library.Ajax.ResponseParser();
                    if (xmlResponse == null)
                        return;

                    var divList = false;
                    for (var i = 0; i < xmlResponse.getElementsByTagName("name").length; i++) {
                        if (xmlResponse.getElementsByTagName("value")[i].firstChild != null) {
                            if (!divList && document.getElementById(xmlResponse.getElementsByTagName("name")[i].firstChild.data).ControlType == 'DivList') {
                                divList = true;
                                Library.Ajax.AddQueue("document.getElementById('" + xmlResponse.getElementsByTagName("name")[i].firstChild.data + "').innerHTML = '';");
                            }
                            Library.Ajax.AddQueue("Library.UserInterface.Set('" + xmlResponse.getElementsByTagName("name")[i].firstChild.data + "', '" + xmlResponse.getElementsByTagName("value")[i].firstChild.data + "');");                            
                            if (document.getElementById(xmlResponse.getElementsByTagName("name")[i].firstChild.data).ControlType == 'Combo' && document.getElementById(xmlResponse.getElementsByTagName("name")[i].firstChild.data).onchange != null) {
                                Library.Ajax.AddQueue("document.getElementById('" + xmlResponse.getElementsByTagName("name")[i].firstChild.data + "').onchange();");
                            }
                        } else {
                            Library.Ajax.AddQueue("Library.UserInterface.Set('" + xmlResponse.getElementsByTagName("name")[i].firstChild.data + "', '');");
                        }
                    }

                    Library.Ajax.RunQueue();

                    if (postScriptName != null && postScriptName != '') {
                        eval(postScriptName);
                    }
                }
            }
            ajaxRequest.open("get", paramUrl, true);
            ajaxRequest.send(null);
        }
        Library.Ajax.Progress(false);
    }
}

/*
* 
* TEST = 
*/
Library.Ajax.AutoComplete = {
    Create: function (sourceControlId, targetControlId, length, url, postScriptName, postMethod) {
        var text = "";
        var autoCompleteSelectedItemIndex = 0;
        var autoCompleteElement = Library.UserInterface.GetDetailPageElement(sourceControlId);


        autoCompleteElement.autocomplete = "off";
        autoCompleteElement.setAttribute("autocomplete", "off");
        autoCompleteElement.onkeyup = function (event) {
            var autoCompleteElement = Library.UserInterface.GetDetailPageElement(sourceControlId);
            if (autoCompleteElement.value != text && autoCompleteElement.value.length >= length) {
                Library.UserInterface.DisableByType("input", "button");
                Library.UserInterface.DisableByType("input", "submit");
                var text = autoCompleteElement.value;
                var key = Library.Util.Event(event).keyCode || Library.Util.Event(event).which;

                if (key != 27 && key != 40 && key != 38 && key != 13) {
                    Library.Ajax.Create();
                    if (ajaxRequest) {
                        var paramUrl = Library.Ajax.ReplaceParameters(url);
                        ajaxRequest.onreadystatechange = function () {
                            if (Library.Ajax.Status()) {
                                var xmlResponse = Library.Ajax.ResponseParser();
                                if (xmlResponse == null)
                                    return;

                                var autoCompleteElement = Library.UserInterface.GetDetailPageElement(sourceControlId);
                                var position = Library.UserInterface.ElementPosition(sourceControlId);
                                var autoCompleteContainer = document.getElementById('AutoCompleteContainer');
                                autoCompleteContainer.style.display = '';
                                autoCompleteContainer.style.position = 'absolute';
                                autoCompleteContainer.style.top = eval(position[0] + 19) + "px";
                                autoCompleteContainer.style.left = eval(position[1]) + "px";
                                autoCompleteContainer.innerHTML = '';

                                var itemCount = 0;
                                if (xmlResponse != null) {
                                    for (var i = 0; i < xmlResponse.getElementsByTagName("name").length; i++) {
                                        var item = "";
                                        item += "<div index='" + i + "' id='autoCompleteItem" + i + "' class='AutoCompleteItem' ";                                        
                                        item += "value='" + xmlResponse.getElementsByTagName("value")[i].firstChild.data + "' ";
                                        item += "onclick=\"Library.Ajax.AutoComplete.Set('" + targetControlId + "', '" + sourceControlId + "', this.index || this.getAttribute('index'), '" + postScriptName + "', '" + postMethod + "'); Library.Ajax.AutoComplete.Hide('" + sourceControlId + "', false); return false;\"";
                                        item += "onmouseover=\"Library.Ajax.AutoComplete.Select(this.index || this.getAttribute('index'));\"";
                                        item += ">" + xmlResponse.getElementsByTagName("name")[i].firstChild.data + "</div>";
                                        autoCompleteContainer.innerHTML += item;
                                        itemCount++;
                                    }
                                }
                                if (itemCount == 0) {
                                    Library.Ajax.AutoComplete.Hide(sourceControlId, false);
                                } else {
                                    autoCompleteSelectedItemIndex = 0;
                                    Library.Ajax.AutoComplete.Select(autoCompleteSelectedItemIndex);
                                }
                            }
                        }
                        ajaxRequest.open("get", paramUrl, true);
                        ajaxRequest.send(null);
                    }
                    Library.Ajax.Progress(false);
                }
            }

            // BACKSPACE
            if (key == 8 && text.length == 0) {
                Library.Ajax.AutoComplete.Hide(sourceControlId, true);
            }

            // ESC
            if (key == 27) {
                Library.Ajax.AutoComplete.Hide(sourceControlId, true);
            }

            // DOWN
            if (key == 40) {
                autoCompleteSelectedItemIndex++;
                Library.Ajax.AutoComplete.Select(autoCompleteSelectedItemIndex);
            }

            // UP
            if (key == 38) {
                autoCompleteSelectedItemIndex--;
                Library.Ajax.AutoComplete.Select(autoCompleteSelectedItemIndex);
            }

            // ENTER
            if (key == 13) {
                Library.Util.Return(event, false)
                Library.Ajax.AutoComplete.Set(targetControlId, sourceControlId, autoCompleteSelectedItemIndex, postScriptName, postMethod);
                Library.Ajax.AutoComplete.Hide(sourceControlId, false);
                return false;
            }
        };

        autoCompleteElement.onblur = function () {
            setTimeout(function () {
                Library.Ajax.AutoComplete.Hide(sourceControlId, false);
            }, 100);
        };
    },

    Hide: function (sourceControlId, deleteTargetValue) {
        if (deleteTargetValue)
            Library.UserInterface.GetDetailPageElement(sourceControlId).value = "";
        document.getElementById('AutoCompleteContainer').style.display = 'none';
        Library.UserInterface.EnableByType("input", "button");
        Library.UserInterface.EnableByType("input", "submit");
    },

    Select: function (index) {
        var autoCompleteContainer = document.getElementById('AutoCompleteContainer');
        var items = autoCompleteContainer.getElementsByTagName('div');
        for (var i = 0; items != null && i < items.length; i++) {
            if (index % items.length == i) {
                items[i].style.backgroundColor = '#006699';
                items[i].style.color = '#FFFFFF';
            } else {
                items[i].style.backgroundColor = '#FCFCFC';
                items[i].style.color = '#000000';
            }
        }
    },

    Set: function (targetControlId, sourceControlId, index, postScriptName, postMethod) {
        var autoCompleteContainer = document.getElementById('AutoCompleteContainer');
        var items = autoCompleteContainer.getElementsByTagName('div');
        for (var i = 0; i < items.length; i++) {
            if (index == i) {
                var value = items[i].value || items[i].getAttribute("value");
                Library.UserInterface.Set(targetControlId, value);
                if (targetControlId != sourceControlId)
                    Library.UserInterface.Set(sourceControlId, items[i].innerHTML);
                break;
            }
        }
        if (postScriptName != null && postScriptName != "") {
            eval(postScriptName);
        }
        if (postMethod != null && postMethod != "") {
            Library.UserInterface.Submit(null, null, postMethod);
            document.forms[0].submit();
        }
    }
};